<?php
/**
 * Created by PhpStorm.
 * User: aDaddy
 * Date: 2016/7/25
 * Time: 9:55
 */

namespace App\Model\LazyCommon;
use App\Base\BaseModel;
use App\Model\LazyCommon\MoneyModel;
use App\Entity\UserInviteBonus;
//use App\Setting\LazyCommon\UserInviteBonusSetting;

//const __UserInvite_Bonus = 100;
//const __UserInvite_FromType_UserReg = 1;
//const __UserInvite_FromType_UserBuy = 2;
class UserInviteBonusModel extends BaseModel{



    public function getRep()
    {
        return  $this->getDoctrine()->getRepository("App:UserInviteBonus");
    }



    /****************************************** 私有 *****************************************/

    /**
     * @param $id
     * @return UserInviteBonus
     */
    public function get($id)
    {
        return $this->getRep()->find($id);
    }

    /**
     * @param $uid
     * @param int $page
     * @param int $limit
     * @return UserInviteBonus[]
     */
    public function getUser($uid,$page = 0 , $limit = 10)
    {
        $UserInviteBonus = $this->getRep()->findBy(array("uid"=> $uid),array("addTime"=>"DESC"),$limit,$page * $limit);

        return $UserInviteBonus;
    }


    public function addUserInviteBonus($uid,$fromId)
    {
//        $inviteBonus = 100;
//        $formType = 1;//1：用户注册

        $UserInviteBonus = new UserInviteBonus();
        $UserInviteBonus->setUid($uid)
            ->setFromId($fromId)
            ->setFromLevel(0)
            ->setFromType(__UserInvite_FromType_UserReg)
            ->setBonus(__UserInvite_Bonus)
            ->setAddTime(time())
            ->setGiveTime(0)
            ->setStatus(0)
            ->setGiveCondition("用户激活");
        $this->em->persist($UserInviteBonus);
        $this->em->flush();


        //判断是否具有领取权限，如果具有直接发放，如果不具有则不处理
//        $MoneyModel = new MoneyModel($this->getDoctrine());
//        $userMoney = $MoneyModel->getUserMoney($uid);
//        $sumIn = $userMoney->getSumIn();
//        if ($sumIn > 99) {
//            $UserInviteBonus->setStatus(1);
//            //添加资金记录
//
//        }
    }



    public function addUserInviteBuyBonus($uid,$fromId,$bonus)
    {

        $UserInviteBonus = new UserInviteBonus();
        $UserInviteBonus->setUid($uid)
            ->setFromId($fromId)
            ->setFromLevel(0)
            ->setFromType(__UserInvite_FromType_UserBuy)
            ->setBonus($bonus)
            ->setAddTime(time())
            ->setGiveTime(0)
            ->setStatus(1)
            ->setGiveCondition("达到相应级别");
        $this->em->persist($UserInviteBonus);
        $this->em->flush();


        //判断是否具有领取权限，如果具有直接发放，如果不具有则不处理
//        $MoneyModel = new MoneyModel($this->getDoctrine());
//        $userMoney = $MoneyModel->getUserMoney($uid);
//        $sumIn = $userMoney->getSumIn();
//        if ($sumIn > 99) {
//            $UserInviteBonus->setStatus(1);
//            //添加资金记录
//
//        }
    }

    /**
     * @param $uid
     * @param int $page
     * @param int $limit
     * @return UserInviteBonus[]
     */
    public function getList($uid,$page = 0 , $limit = 50)
    {
        $rep  = $this->getRep();
        $rst = $rep->findBy(array("uid"=>$uid),array("addTime"=>"DESC"),$limit,$page*$limit);
        return $rst;

    }

    /**
     * @param $fromId
     * @param $fromType
     * @return UserInviteBonus
     */
    public function getByFromIdAndFromType($fromId,$fromType)
    {
        $rep = $this->getRep();
        return $rep->findOneBy(array("fromId"=>$fromId,"fromType"=>$fromType));
    }


    public function getStatistic($uid,$status){
        $qb = $this->getQueryBuilder();
        $qb->select("sum(A.bonus) as sum")->from("UserInviteBonus","A");
        $qb->where("A.uid = ".(int)$uid);
        $qb->andWhere("A.status = ".(int)$status);
        $rst = $qb->execute()->fetchAll();
        return $rst[0]["sum"];
    }


    public function dealWithUserActive($uid)
    {
        $UserInviteModel = new UserInviteModel($this->getDoctrine());
        $parents =$UserInviteModel->getUserParentsIdArray($uid);
        if (isset($parents[0]) && $parents[0]) {
            $parentId = $parents[0];
            //更新UserInviteBonus中的status
            $row = $this->getByFromIdAndFromType($uid,1);//fromType = 1邀请奖励，2：佣金奖励
            if ($row && $row->getStatus() == 0) {
                $row->setStatus(1);
                $this->em->persist($row);

                //为用户增加奖励
                $MoneyModel = new MoneyModel($this->getDoctrine());
                $MoneyModel->addMoney4UserInvite($parentId,__UserInvite_Bonus);
            }
        }
    }

}